<?php
/**
 * Created by PhpStorm
 * User: whh
 * Date: 2022/7/28
 * Time: 16:12
 */

namespace app\reception\controller;

use app\backstage\model\Faq;
use app\common\model\Bookshelf;
use app\reception\logic\UserLogic;
use think\Db;
use think\Exception;

class User extends Common
{
    protected $middleware = [
        'VerifyAuth' => ['except' => ['auth']],
    ];

    /**
     * 用户授权
     * @return \think\response\Json
     * @Author: whh
     */
    public function auth()
    {
        $params = $this->request->param();
        $result = $this->validate($params, 'app\reception\validate\UserValidate.auth');
        if (true !== $result) {
            return errJson($result);
        }
        $userLogic = new \app\reception\logic\UserLogic();
        $res = $userLogic->auth($this->quickAppId, $params);
        if ($res['code'] == 1) {
            return sucJson(['user_token' => $res['token'], 'is_market' => $res['is_market']]);
        }
        return errJson($res['msg']);
    }


    /**
     * 加桌
     * @return \think\response\Json
     * @Author: whh
     */
    public function addShortcut()
    {
        $userLogic = new UserLogic();
        $userLogic->addShortcut($this->user);
        return sucJson();
    }


    /**
     * 设置服务商
     * @return \think\response\Json
     */
    public function setProvider()
    {
        $params = $this->request->param();
        $result = $this->validate($params, 'app\reception\validate\UserValidate.setProvider');
        if (true !== $result) {
            return errJson($result);
        }
        $provider = input('provider');
        $userLogic = new \app\reception\logic\UserLogic();
        $userLogic->setProvider($this->user, $provider);
        return sucJson();
    }

    /**
     * 订阅
     * @return \think\response\Json
     * @Author: whh
     */
    public function subscribe()
    {
        $params = $this->request->param();
        $result = $this->validate($params, 'app\reception\validate\UserValidate.subscribe');
        if (true !== $result) {
            return errJson($result);
        }
        $userLogic = new \app\reception\logic\UserLogic();
        $userLogic->subscribe($this->user, $params['regId']);
        return sucJson();
    }

    /**
     * 发送短信
     * @return array|\think\response\Json
     * @Author: whh
     */
    public function smsCode()
    {
        $params = $this->request->param();
        $result = $this->validate($params, 'app\reception\validate\UserValidate.smsCode');
        if (true !== $result) {
            return errJson($result);
        }
        $userLogic = new UserLogic();
        $res = $userLogic->sendSms($this->quickAppId, $this->uid, $params['phone']);
        if ($res['code'] == 1) {
            return sucJson();
        }
        return errJson($res['msg']);
    }


    /**
     * 绑定
     * @return \think\response\Json
     * @Author: whh
     */
    public function binding()
    {
        $params = $this->request->param();
        $result = $this->validate($params, 'app\reception\validate\UserValidate.binding');
        if (true !== $result) {
            return errJson($result);
        }
        $userLogic = new UserLogic();
        return $userLogic->binding($this->user, $params['phone'], $params['code']);
    }


    /**
     * 个人中心
     * @return \think\response\Json
     */
    public function profile(){
        $time = time();
        $userPointModel = model('UserBookPoints');
        $data = [
            'id' => $this->user->id,
            'nickname' => $this->user->nickname,
            'phone' => $this->user->phone,
            'vip_exp_time' => $this->user->vip_exp_time * 1000,//转毫秒
            'book_coin' => $this->user->book_coin,
            'book_point' => $userPointModel->getBalancePoint($this->user->id),
            'is_market' => $this->user->channel_id ? 0 : 1,//是否为市场用户
        ];
        return sucJson($data);
    }

    /**
     * 阅读记录
     * @return \think\response\Json
     */
    public function readRecord(){
        $userLogic = new UserLogic();
        return $userLogic->getBookshelf($this->user,$this->isWhite,$this->isBlack);
    }

    /**
     * 删除阅读记录，软删除
     * @return \think\response\Json
     */
    public function delReadRecord(){
        $params = $this->request->param();
        $result = $this->validate($params, 'app\reception\validate\UserValidate.delReadRecord');
        if (true !== $result) {
            return errJson($result);
        }
        $nids = input('nids');
        $where[] = ['uid', '=', $this->uid];
        $where[] = ['novel_id', 'in', $nids];
        $list = Bookshelf::where($where)->select();
        foreach ($list as $bs){
            $bs->delete();
        }
        return sucJson();
    }

    /**
     * 用户反馈
     * @return \think\response\Json
     */
    public function feedback(){
        $userLogic = new UserLogic();
        return $userLogic->getFeedbackList($this->user);
    }

    /**
     * 添加反馈
     * @return \think\response\Json
     */
    public function addFeedBack(){
        $params = $this->request->param();
        $result = $this->validate($params, 'app\reception\validate\UserValidate.addFeedBack');
        if (true !== $result) {
            return errJson($result);
        }
        $content = input('content');
        $faq = new Faq();
        $faq->quick_id = $this->quickAppId;
        $faq->uid = $this->uid;
        $faq->content = $content;
        $faq->type = 1;
        $faq->from_id = 0;
        $faq->user_reply_time = time();
        $faq->have_read = 0;
        $faq->save();
        return sucJson();
    }

    /**
     * 添加评论
     * @return \think\response\Json
     */
    public function addComments(){
        $params = $this->request->param();
        $result = $this->validate($params, 'app\reception\validate\UserValidate.addComments');
        if (true !== $result) {
            return errJson($result);
        }
        $content = input('content');
        $from_id = input('from_id');
        $parent = Faq::get($from_id);
        if (!$parent){
            return errJson('源反馈不存在');
        }
        if ($parent->uid != $this->uid){
            return errJson('from_id异常');
        }
        $time = time();
        $faq = new Faq();
        $faq->quick_id = $this->quickAppId;
        $faq->uid = $this->uid;
        $faq->content = $content;
        $faq->type = 2;
        $faq->from_id = $from_id;
        $faq->user_reply_time = $time;
        $faq->have_read = 0;
        $faq->save();
        $parent->user_reply_time = $time;
        $parent->have_read = 0;
        $parent->save();
        return sucJson();
    }

    /**
     * 消费列表
     * @return \think\response\Json
     * @Author: whh
     */
    public function consumeRecord()
    {
        $params = $this->request->param();
        $result = $this->validate($params, 'app\reception\validate\UserValidate.consumeRecord');
        if (true !== $result) {
            return errJson($result);
        }
        $month = input('month', date("Ym"));
        $page = input('page',1);
        $limit = input('limit',10);
        try {
            $where[] = ['uid', '=', $this->uid];
            $where[] = ['pay_read', '=', 1];
            $field = "novel_id,chapter_no,used_all,used_point,used_coin,read_time";
            $list = Db::table("quick_user_read_record_{$month}")->field($field)->where($where)->page($page, $limit)->order("id desc")->select();
            foreach ($list as $k => $v){
                $list[$k]['novel_name'] = Db::table('novel')->where("id",$v['novel_id'])->value('origin_title');
                $list[$k]['chapter_name'] = Db::table('novel_chapter')->where("novel_id", $v['novel_id'])->where("chapter_no", $v['chapter_no'])->value('name');
            }
            $count = Db::table("quick_user_read_record_{$month}")->where($where)->count();
            return sucJson($list, $count);
        } catch (Exception $e) {
            return sucJson([], 0);
        }
    }
}